package com.example.bootdemo.exception;


import com.example.bootdemo.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.sql.SQLSyntaxErrorException;


/**
 * @author yy
 */
@RestControllerAdvice
public class RRExceptionHandler {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(RRException.class)
    public R handleRRException(RRException e) {
        R r = new R();
        r.put("code", e.getCode());
        r.put("msg", e.getMessage());

        return r;
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public R handlerNoFoundException(NoHandlerFoundException e) {
        logger.error(e.getMessage(), e);
        return R.error(404, "路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(NullPointerException.class)
    public R NullPointerException(NullPointerException e) {
        logger.error(e.getMessage(), e);
        return R.error(500, "空指针异常");
    }

    @ExceptionHandler(ArithmeticException.class)
    public R ArithmeticException(ArithmeticException e) {
        logger.error(e.getMessage(), e);
        return R.error("数据计算异常");
    }

    @ExceptionHandler(SQLSyntaxErrorException.class)
    public R MySQLSyntaxErrorException(SQLSyntaxErrorException e) {
        logger.error(e.getMessage(), e);
        return R.error("SQL异常");
    }


    @ExceptionHandler(DuplicateKeyException.class)
    public R handleDuplicateKeyException(DuplicateKeyException e) {
        logger.error(e.getMessage(), e);
        return R.error("数据库中已存在该记录");
    }

    @ExceptionHandler(NumberFormatException.class)
    public R handleNumberFormatException(NumberFormatException e) {
        logger.error(e.getMessage(), e);
        return R.error("数字转换异常");
    }

    /*	@ExceptionHandler(MySQLIntegrityConstraintViolationException.class)
        public R handleMySQLIntegrityConstraintViolationException(MySQLIntegrityConstraintViolationException e){
            logger.error(e.getMessage(), e);
            return R.error("数据不能为空");
        }*/
    @ExceptionHandler(StringIndexOutOfBoundsException.class)
    public R handleStringIndexOutOfBoundsException(StringIndexOutOfBoundsException e) {
        logger.error(e.getMessage(), e);
        return R.error("数组下标越界异常");
    }

    @ExceptionHandler(ClassCastException.class)
    public R handleClassCastException(ClassCastException e) {
        logger.error(e.getMessage(), e);
        return R.error("类型转换异常");
    }

    @ExceptionHandler(BadSqlGrammarException.class)
    public R myBadSqlGrammarException(BadSqlGrammarException e) {
        logger.error(e.getMessage(), e);
        return R.error("SQL执行异常");
    }


    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        logger.error(e.getMessage(), e);
        return R.error("请求方式不对");
    }

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return R.error();
    }


}
